package com.ksm.zyz.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ksm.zyz.common.base.BaseController;
import com.ksm.zyz.sys.entity.BaseGoodsStock;
import com.ksm.zyz.sys.entity.ExtData;
import com.ksm.zyz.sys.serverentity.Tongbu;
import com.ksm.zyz.sys.serverservice.TongbuService;
import com.ksm.zyz.sys.serverservice.sBaseGoodsStockService;
import com.ksm.zyz.sys.serverservice.sXinXiBiaoService;
import com.ksm.zyz.sys.service.baseGoodsStockService;
import com.ksm.zyz.sys.service.mySqlTongbuService;

/**
 * @author liuhao
 *
 */
@Controller
@RequestMapping("guodu")
public class guoduController extends BaseController<Tongbu> {

	@Autowired
	private TongbuService Service;
	@Autowired
	private sXinXiBiaoService xService;
	@Autowired
	private mySqlTongbuService mySqlTongbuService;
	@Autowired
	private sBaseGoodsStockService sBaseGoodsStockService;
	@Autowired
	private baseGoodsStockService BaseGoodsStockService;

	
	
	   @RequestMapping(value ="tongbu")
	    @ResponseBody
		public ExtData tongbu(int ming){
		   try{
			 //数据包的封装
			 // 分为  添加数据 删除 数据  和修改数据
			 // 例：//map("insert",insertmap<"BaseGoodsStock",List<BaseGoodsStock>>)
			 
			 
			 Map<String,List> insertmap=new HashMap<String,List>();
			 Map<String,List> deletemap=new HashMap<String,List>();
			 Map<String,List> updatemap=new HashMap<String,List>();
			 
			 //BaseGoodsStock
			 List<BaseGoodsStock> insertBaseGoodsStocklist=new ArrayList<BaseGoodsStock>();
			 List<BaseGoodsStock> deleteBaseGoodsStocklist=new ArrayList<BaseGoodsStock>();
			 List<BaseGoodsStock> updateBaseGoodsStocklist=new ArrayList<BaseGoodsStock>();

			 			 			 
			 List<Tongbu> tlist= Service.selectAllNew(0);
			 int size=tlist.size();
			 for(int i=0;i<size;i++)
			  {
			    String operate=tlist.get(i).getOperate();
				String Biaoming=tlist.get(i).getBiaoming();
				String idkey=tlist.get(i).getIdkey();
								
				if(".".equals(Biaoming))
				{
					
				}				
			 else if("base_goods_stock".equals(Biaoming))
				{
					if("inserted".equals(operate))
					{ 
					  BaseGoodsStock info=new BaseGoodsStock();
					  //int id=Integer.parseInt(idkey);
					  info.setGoodsNum(idkey);
					  info.setSmId(ming);
					  BaseGoodsStock inf=BaseGoodsStockService.selectInfo(info);
					  inf.setId(null);
					  insertBaseGoodsStocklist.add(inf);
					}
					else if("delete".equals(operate))
					{
					  BaseGoodsStock info=new BaseGoodsStock();
					  //int id=Integer.parseInt(idkey);
					  info.setGoodsNum(idkey);
					  info.setSmId(ming);
					  deleteBaseGoodsStocklist.add(info);
					}
					else if("updated".equals(operate))
					{
					  BaseGoodsStock info=new BaseGoodsStock();
					  //int id=Integer.parseInt(idkey);
					  info.setGoodsNum(idkey);
					  info.setSmId(ming);
					  BaseGoodsStock inf=BaseGoodsStockService.selectInfo(info);
					  inf.setId(null);
					  updateBaseGoodsStocklist.add(inf);
					}
				}
						
			  }
			 
			 if(insertBaseGoodsStocklist.size()!=0)
			 {
			 insertmap.put("BaseGoodsStock", insertBaseGoodsStocklist);
			 }
			 if(deleteBaseGoodsStocklist.size()!=0)
			 {
			deletemap.put("BaseGoodsStock", deleteBaseGoodsStocklist); 
			 }
			 if(updateBaseGoodsStocklist.size()!=0)
			 {
			updatemap.put("BaseGoodsStock", updateBaseGoodsStocklist); 
			 }
			 
			 
			 
			 
			 Map<String,Map> map=new HashMap<String, Map>();
			 if(insertmap.size()!=0)
			 map.put("insertmap", insertmap);
			 if(deletemap.size()!=0)
			 map.put("deletemap", deletemap);
			 if(updatemap.size()!=0)
			 map.put("updatemap", updatemap);
			 
			 System.out.println(map);
			 ObjectMapper mapper=new ObjectMapper();
	         String json1=mapper.writeValueAsString(map);
	         System.out.println(json1);
	         //String json=URLEncoder.encode(json1, "utf-8"); 
	         String json=new String(json1.getBytes("utf-8"),"utf-8"); 
	         System.out.println(json);
	         
	         List li=new ArrayList<Map>();
	         li.add(map);
	         extData.setList(li);
	         
			return extData;
		 }
				
		  catch (Exception e) {  
	            e.printStackTrace();  
	            return null;
	        }
		   
	     
		
	   

}
}
